import * as VueRouter from 'vue-router';

// meta.title 必须要配置，不然路由守卫会报错
const routes: VueRouter.RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/login',
    component: () => import('@/components/layout/page'),
    children: [
      {
        path: 'login',
        component: () => import('@/views/login/index.<%= typescript
        ? 't'
        : 'j' %>sx'),
        meta: {
          title: '登录',
        },
      },
    ],
  },
  {
    path: '/main',
    component: () => import('@/components/layout/tabBar'),
    children: [
      {
        path: '/home',
        component: () => import('@/views/home/index.vue'),
        meta: {
          title: '首页',
        },
      },
      {
        path: '/about',
        component: () => import('@/views/about/index.vue'),
        meta: {
          title: '关于',
        },
      },
    ],
  },
];

const router = VueRouter.createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history: VueRouter.createWebHashHistory(),
  routes, // `routes: routes` 的缩写
});

router.afterEach((to) => {
  const {
    meta: { title },
  } = to;
  document.title = (title as string) || '活动报名';
});

export default router;
